/* $Id: hanyang.js  hong $ */
var process_request = "{$lang.process_request}";

function addEvent(el,type,handle){   
    if(el.addEventListener){//for standard browses   
        el.addEventListener(type,handle,false);   
    }else if(el.attachEvent){//for IE   
        el.attachEvent("on"+event,handle);   
    }else{//other   
        el["on"+type]=handle;   
    }     
}

function changePagesize(){	
	document.forms['frm_page'].submit();        
}

function isIE(){   
    return navigator.appName.indexOf("Microsoft Internet Explorer")!=-1 && document.all;   
}   
function isIE6() {   
    return navigator.userAgent.split(";")[1].toLowerCase().indexOf("msie 6.0")=="-1"?false:true;   
}   
function isIE7(){   
    return navigator.userAgent.split(";")[1].toLowerCase().indexOf("msie 7.0")=="-1"?false:true;   
}   
function isIE8(){   
    return navigator.userAgent.split(";")[1].toLowerCase().indexOf("msie 8.0")=="-1"?false:true;   
}   
function isNN(){   
    return navigator.userAgent.indexOf("Netscape")!=-1;   
}   
function isOpera(){   
    return navigator.appName.indexOf("Opera")!=-1;   
}   
function isFF(){   
    return navigator.userAgent.indexOf("Firefox")!=-1;   
}   
function isChrome(){   
    return navigator.userAgent.indexOf("Chrome") > -1;     
}

function parse_float(val)
{//转换成浮点数	
	if(isNaN(parseFloat(val))){
		return Number(0);
	}	
	//var num=new Number(val);
	//alert(typeof num.toFixed(2));
	return parseFloat(val).toFixed(2);
}


//检测图片大小，尺寸--------------------------
	
	var ImgObj=new Image(); //建立一个图像对象 
	var AllImgExt=".jpg|.jpeg|.gif|.bmp|.png|"//全部图片格式类型 
	var FileObj,ImgFileSize,ImgWidth,ImgHeight,FileExt,ErrMsg,FileMsg,HasCheked,IsImg//全局变量 图片相关属性 
	//以下为限制变量 
	var AllowExt=".jpg|.gif|.bmp|" //允许上传的文件类型 ?为无限制 每个扩展名后边要加一个"|" 小写字母表示 
	//var AllowExt=0 
	var AllowImgFileSize=1024; //允许上传图片文件的大小 0为无限制 单位：KB 
	var AllowImgWidth=780; //允许上传的图片的宽度 ?为无限制 单位：px(像素) 
	var AllowImgHeight=600; //允许上传的图片的高度 ?为无限制 单位：px(像素) 
	HasChecked=false; 
function CheckProperty(obj){  //检测图像属性 
	FileObj=obj; 
	if(ErrMsg!=""){ 
	//检测是否为正确的图像文件 返回出错信息并重置 
	alert(ErrMsg)
	return false; //返回 
	} 
	if(ImgObj.readyState!="complete") {
	//如果图像是未加载完成进行循环检测 
	setTimeout("CheckProperty(FileObj)",500); 
	return false; 
	} 
	ImgFileSize=Math.round(ImgObj.fileSize/1024*100)/100;//取得图片文件的大小 
	ImgWidth=ImgObj.width //取得图片的宽度 
	ImgHeight=ImgObj.height; //取得图片的高度 
	FileMsg="\n图片大小:"+ImgWidth+"*"+ImgHeight+"px"; 
	FileMsg=FileMsg+"\n图片文件大小:"+ImgFileSize+"Kb"; 
	FileMsg=FileMsg+"\n图片文件扩展名:"+FileExt; 
	if(AllowImgWidth!=0&&AllowImgWidth<ImgWidth) 
	ErrMsg=ErrMsg+"\n图片宽度超过限制。请上传宽度小于"+AllowImgWidth+"px的文件，当前图片宽度为"+ImgWidth+"px"; 
	if(AllowImgHeight!=0&&AllowImgHeight<ImgHeight) 
	ErrMsg=ErrMsg+"\n图片高度超过限制。请上传高度小于"+AllowImgHeight+"px的文件，当前图片高度为"+ImgHeight+"px"; 
	if(AllowImgFileSize!=0&&AllowImgFileSize<ImgFileSize) 
	ErrMsg=ErrMsg+"\n图片文件大小超过限制。请上传小于"+AllowImgFileSize+"KB的文件，当前文件大小为"+ImgFileSize+"KB"; 
	if(ErrMsg!=""){
	 alert(ErrMsg);
	}else 
	 alert("该图片可以上传\n"+FileMsg)
}  
ImgObj.onerror=function(){ErrMsg='\n图片格式不正确或者图片已损坏!'} 
function ShowMsg(msg,tf) //显示提示信息 tf=true 显示文件信息 tf=false 显示错误信息 msg-信息内容 
{ 
	//msg=msg.replace("\n","<li>"); 
	//msg=msg.replace(/\n/gi,"<li>"); 
	if(!tf) 
	{ 
	  alert(msg)
	  HasChecked=false; 
	} 
	else 
	{ 
	if(IsImg){} 
	//alert('是图片文件宽：'+ImgWidth+"高："+ImgHeight+"size:"+ImgFileSize)
	else 
	alert(msg);
	HasChecked=true;
	} 
} 
function check_img(obj) 
{ 
	ErrMsg=""; 
	FileMsg=""; 
	FileObj=obj; 
	IsImg=false; 
	HasChecked=false; 
	if(obj.value=="")return false; 
	FileExt=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase(); 
	if(AllowExt!=0&&AllowExt.indexOf(FileExt+"|")==-1) //判断文件类型是否允许上传 
	{ 
	ErrMsg="\n该文件类型不允许上传。请上传 "+AllowExt+" 类型的文件，当前文件类型为"+FileExt; 
	alert(ErrMsg);
	return false; 
	} 
	if(AllImgExt.indexOf(FileExt+"|")!=-1) //如果图片文件，则进行图片信息处理 
	{ 
	IsImg=true; 
	ImgObj.src=obj.value; 
	CheckProperty(obj); 
	return false; 
	} 
	else 
	{ 
	FileMsg="\n文件扩展名:"+FileExt;
	alert(FileMsg)
	} 
}

//检测图片大小，尺寸--------------------------

String.prototype.Trim   =   function()  
  {  //删除左右两端的空格
  return   this.replace(/(^\s*)|(\s*$)/g,   "");  
  }  
function getlength(str) {
    ///<summary>获得字符串实际长度，中文2，英文1</summary>
    ///<param name="str">要获得长度的字符串</param>
    var realLength = 0, len = str.length, charCode = -1;
    for (var i = 0; i < len; i++) {
        charCode = str.charCodeAt(i);
        if (charCode >= 0 && charCode <= 128) realLength += 1;
        else realLength += 2;
    }
    return realLength;
}
function getfileName(url){
	//var url = this.location.href
	filename='';
	if(url==''){return filename;}
	var pos = url.lastIndexOf("/");
	if(pos == -1){
	pos = url.lastIndexOf("\\")
	}
	var filename = url.substr(pos +1)
	pos = filename.indexOf(".php");
	filename = filename.substr(0,pos)
	return filename;
}
function getfileName_this(){
    var url = this.location.href;
	return getfileName(url);
}
function show_this_menu(){
    window.parent.open_menu_s('join_in');
	var this_file=getfileName_this();
	var obj_id="menu_"+this_file;
	var obj=window.parent.document.getElementById(obj_id);
	window.parent.set_class(obj);
}
function input_select(select_id,data_arr){
	if(select_id==''){return;}
	var eles = window.document.getElementById(select_id);
	//清除列表
	 var selLen = eles.options.length;
      for (var i = selLen - 1; i >= 0; i--)
      {
        eles.options[i] = null;
      }
	//清除列表
      var arr = data_arr;
      var goodsCnt = arr.length;
      if (goodsCnt > 0)
      {
        for (var i = 0; i < goodsCnt; i++)
        {
          var opt = document.createElement('OPTION');
          opt.value = arr[i][0];
          opt.text = arr[i][1];
          eles.options.add(opt);
        }
	  }
	
}
function hideAllSelect(hSwitch){ 
    if (hSwitch){
        var selects=document.getElementsByTagName("SELECT"); 
        for(var i=0;i <selects.length;i++){selects[i].style.display="none";} 
    }else{
        var selects=document.getElementsByTagName("SELECT"); 
        for(var i=0;i <selects.length;i++){
			selects[i].style.display="";
		}
    }
} 

function show_sel(sel_id){
    document.getElementById(sel_id).style.display="";
}
function display_none(sel_id){
    document.getElementById(sel_id).style.display="none";
}
function display(sel_id){
    document.getElementById(sel_id).style.display="";
}
function disable(sel_id){
    document.getElementById(sel_id).disabled=true;
}
function enable(sel_id){
    document.getElementById(sel_id).disabled=false;
}
function get_select_txt(select_id){
	var txt="";
	if(select_id==''){return txt;}
	if(document.getElementById(select_id).value==''){return txt;}
	try{
		if(isIE()){
		    txt=document.getElementById(select_id).options[document.getElementById(select_id).selectedIndex].innerText;
	    }
	    if(isFF()){
		    txt=document.getElementById(select_id).options[document.getElementById(select_id).selectedIndex].textContent;
	    }
		
	}catch(ex){}
	
	return txt;
}
function set_select_txt(select_id,target_id){
	if(select_id==''||target_id==''){return false;}
	document.getElementById(target_id).value=get_select_txt(select_id);
	return true;
}
function checked_all(obj,to_id){
	if(obj==''||to_id==''){return false;}
	var checked_val=obj.checked
	var elems = document.getElementsByTagName("INPUT");
	length_obj=elems.length;

	for(i=0;i<length_obj;i++){
	 //if(elems[i].id==to_id+"["+i+"]"){
		try{
			document.getElementById(to_id+"["+i+"]").checked=checked_val;
		}catch (ex){}
	  //}
	}
}

function china_week(eng_week){
	var china_week_name="";
	switch(eng_week){
		case '':
		    china_week_name="";
			break;
		case 'Monday':
		    china_week_name="星期一";
			break;
		case 'Tuesday':
		    china_week_name="星期二";
			break;
		case 'Wednesday':
		    china_week_name="星期三";
			break;
		case 'Thursday':
		    china_week_name="星期四";
			break;
		case 'Friday':
		    china_week_name="星期五";
			break;
		case 'Saturday':
		    china_week_name="星期六";
			break;
		case 'Sunday':
		    china_week_name="星期日";
		}
	return china_week_name;
}
function checkIdcard(idcard){ //检查身份证合法性
	var isIDCard1 = new Object();
	var isIDCard2 = new Object();
	//str=toLowerCase(str); 
	//身份证正则表达式(15位) 
	isIDCard1=/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/; 
	//身份证正则表达式(18位) 
	isIDCard2=/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[A-Z])$/; 
	//验证身份证，如果正确，提交到reg.asp页面
	if (isIDCard1.exec(idcard)||isIDCard2.exec(idcard))
	{    
	   return true;
	}else{
	   return false;
	}
} 
function check_n_i_input(val){ //正则数字，字母，下划线，横杠输入
    val=val.Trim();
    var ereg=/^[0-9a-zA-Z_-]{1,}$/;
	if (ereg.exec(val)||ereg.exec(val))
	{    
	   return true;
	}else{
	   return false;
	}
}
function set_cpid(cp_id){
    var cpid=cp_id.value;
	var cpname=get_select_txt('cp_id');
	Ajax.call( 'hanyang_ajax.php?act=set_cpid','cpid='+cpid+'&cpname='+cpname, show_mission , 'POST', 'JSON', false);
}


function del_div(son_id,father_id){
	var father=document.getElementById(father_id);
	var son=document.getElementById(son_id);
	father.removeChild(son);
}



function frm_reset(frm){
    if(frm==''){return;}
    document.getElementById(frm).reset();
}

function goto_url(url,target){
	 if(target=="parent"){
		 parent.location=url;
	 }else{
		 window.location=url;
	  }
}


function close_div(obj){
	hideAllSelect(0);
	window.document.getElementById(obj).style.display="none";
}
function show_div(obj){
	hideAllSelect(1);
	window.document.getElementById(obj).style.display="";
}

function visit_div(obj){
	
	window.document.getElementById(obj).style.visibility="visible";
}
function hidden_div(obj){
	window.document.getElementById(obj).style.visibility="hidden";
}



function add_cp_is_reg(username){//检测企业超管帐号
	 r=/^([a-zA-z_]{1})([\w]*)$/
	 username=username.Trim();
	 if(r.exec(username)){
			Ajax.call( 'user.php?act=is_registered', 'username=' + username, add_cp_reg_callback , 'GET', 'TEXT', true, true );
		}
}

function barcode_prefix_exist(barcode_prefix,cp_id){	
	if(barcode_prefix.Trim()!=''){
		Ajax.call( 'hanyang_ajax.php?act=barcode_prefix_exist', 'prefix=' + barcode_prefix.Trim()+"&cp_id="+cp_id.Trim(), barcode_prefix_exist_callback , 'GET', 'TEXT', true, true );
	}	
}

function barcode_prefix_exist_callback($result){
	
}

function show_image(img_url){
	hideAllSelect(1);
	window.document.getElementById('show_img').style.display="";
    window.document.getElementById('show_img').innerHTML="<img src=\""+img_url+"\"  style=\"border:none;\"/>";	
}


function confirm_url(url,msg){
	if(url==''){
		return false;
	}
	if(msg==''){
	   msg="确定要执行该操作吗？";	
	}
	if(confirm(msg)){
		window.location=url;
	}
}

function set_selected_text(select_id,text){	
	var obj=document.getElementById(select_id);
	if(obj=='undefined'||obj==null)return;
	for(var i=0;i<obj.options.length;i++){
		if(obj.options[i].text==text){
			obj.options[i].selected=true;
			break;
		}
	}	
}

function set_selected_value(select_id,value){	
	var obj=document.getElementById(select_id);
	if(obj=='undefined'||obj==null)return;
	for(var i=0;i<obj.options.length;i++){
		if(obj.options[i].value==value){
			obj.options[i].selected=true;
			break;
		}
	}	
}

//select
function FixWidth(selectObj)
{
    var newSelectObj = document.createElement("select");
    newSelectObj = selectObj.cloneNode(true);
    newSelectObj.selectedIndex = selectObj.selectedIndex;
    newSelectObj.id="newSelectObj";
  
    var e = selectObj;
    var absTop = e.offsetTop;
    var absLeft = e.offsetLeft;
    while(e = e.offsetParent)
    {
        absTop += e.offsetTop;
        absLeft += e.offsetLeft;
    }
    with (newSelectObj.style)
    {
        position = "absolute";
        top = absTop + "px";
        left = absLeft + "px";
        width = "auto";
    }
   
    var rollback = function(){ RollbackWidth(selectObj, newSelectObj); };
    if(window.addEventListener)
    {
        newSelectObj.addEventListener("blur", rollback, false);
        newSelectObj.addEventListener("change", rollback, false);
    }
    else
    {
        newSelectObj.attachEvent("onblur", rollback);
        newSelectObj.attachEvent("onchange", rollback);
    }
   
    selectObj.style.visibility = "hidden";
    document.body.appendChild(newSelectObj);
   
    var newDiv = document.createElement("div");
    with (newDiv.style)
    {
        position = "absolute";
        top = (absTop-10) + "px";
        left = (absLeft-10) + "px";
        width = newSelectObj.offsetWidth+20;
        height= newSelectObj.offsetHeight+20;;
        background = "transparent";
        //background = "green";
    }
    document.body.appendChild(newDiv);
    newSelectObj.focus();
    var enterSel="false";
    var enter = function(){enterSel=enterSelect();};
    newSelectObj.onmouseover = enter;
   
    var leavDiv="false";
    var leave = function(){leavDiv=leaveNewDiv(selectObj, newSelectObj,newDiv,enterSel);};
    newDiv.onmouseleave = leave;
}

function RollbackWidth(selectObj, newSelectObj)
{
    selectObj.selectedIndex = newSelectObj.selectedIndex;
    selectObj.style.visibility = "visible";
    if(document.getElementById("newSelectObj") != null){
       document.body.removeChild(newSelectObj);
    }
}

function removeNewDiv(newDiv)
{
    document.body.removeChild(newDiv);
}

function enterSelect(){
  return "true";
}

function leaveNewDiv(selectObj, newSelectObj,newDiv,enterSel){
 if(enterSel == "true" ){
  RollbackWidth(selectObj, newSelectObj);
  removeNewDiv(newDiv);
 }
}


function getElementPos(elementId) {//获取指定id的元素的xy位置返回由x,y组成的数组
	 var ua = navigator.userAgent.toLowerCase();
	 var isOpera = (ua.indexOf('opera') != -1);
	 var isIE = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof
	 var el = document.getElementById(elementId);
	 if(el.parentNode === null || el.style.display == 'none') {
	  return false;
	 }      
	 var parent = null;
	 var pos = [];     
	 var box;     
	 if(el.getBoundingClientRect)    //IE
	 {         
	  box = el.getBoundingClientRect();
	  var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
	  var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
	  return {x:box.left + scrollLeft, y:box.top + scrollTop};
	 }else if(document.getBoxObjectFor)    // gecko    
	 {
	  box = document.getBoxObjectFor(el); 
	  var borderLeft = (el.style.borderLeftWidth)?parseInt(el.style.borderLeftWidth):0; 
	  var borderTop = (el.style.borderTopWidth)?parseInt(el.style.borderTopWidth):0; 
	  pos = [box.x - borderLeft, box.y - borderTop];
	 } else    // safari & opera    
	 {
	  pos = [el.offsetLeft, el.offsetTop];  
	  parent = el.offsetParent;     
	  if (parent != el) { 
	   while (parent) {  
		pos[0] += parent.offsetLeft; 
		pos[1] += parent.offsetTop; 
		parent = parent.offsetParent;
	   }  
	  }   
	  if (ua.indexOf('opera') != -1 || ( ua.indexOf('safari') != -1 && el.style.position == 'absolute' )) { 
	   pos[0] -= document.body.offsetLeft;
	   pos[1] -= document.body.offsetTop;         
	  }    
	 }              
	 if (el.parentNode) { 
		parent = el.parentNode;
	   } else {
		parent = null;
	   }
	 while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML') { // account for any scrolled ancestors
	  pos[0] -= parent.scrollLeft;
	  pos[1] -= parent.scrollTop;
	  if (parent.parentNode) {
	   parent = parent.parentNode;
	  } else {
	   parent = null;
	  }
	 }
	 return {x:pos[0], y:pos[1]};
}



//计算天数差的函数，通用  
   function  DateDiff(sDate1,  sDate2){    //sDate1和sDate2是2006-12-18格式  
       var  aDate,  oDate1,  oDate2,  iDays  
       aDate  =  sDate1.split("-")  
       oDate1  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])    //转换为12-18-2006格式  
       aDate  =  sDate2.split("-")  
       oDate2  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])  
       iDays  =  parseInt(Math.abs(oDate1  -  oDate2)  /  1000  /  60  /  60  /24)    //把相差的毫秒数转换为天数  
       return  iDays  
   }